package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.Stewarder;
import com.mycompany.myapp.repository.StewarderRepository;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Stewarder.
 */
@RestController
@RequestMapping("/api")
public class StewarderResource {

    private final Logger log = LoggerFactory.getLogger(StewarderResource.class);

    private static final String ENTITY_NAME = "stewarder";

    private final StewarderRepository stewarderRepository;

    public StewarderResource(StewarderRepository stewarderRepository) {
        this.stewarderRepository = stewarderRepository;
    }

    /**
     * POST  /stewarders : Create a new stewarder.
     *
     * @param stewarder the stewarder to create
     * @return the ResponseEntity with status 201 (Created) and with body the new stewarder, or with status 400 (Bad Request) if the stewarder has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/stewarders")
    @Timed
    public ResponseEntity<Stewarder> createStewarder(@RequestBody Stewarder stewarder) throws URISyntaxException {
        log.debug("REST request to save Stewarder : {}", stewarder);
        if (stewarder.getId() != null) {
            throw new BadRequestAlertException("A new stewarder cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Stewarder result = stewarderRepository.save(stewarder);
        return ResponseEntity.created(new URI("/api/stewarders/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /stewarders : Updates an existing stewarder.
     *
     * @param stewarder the stewarder to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated stewarder,
     * or with status 400 (Bad Request) if the stewarder is not valid,
     * or with status 500 (Internal Server Error) if the stewarder couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/stewarders")
    @Timed
    public ResponseEntity<Stewarder> updateStewarder(@RequestBody Stewarder stewarder) throws URISyntaxException {
        log.debug("REST request to update Stewarder : {}", stewarder);
        if (stewarder.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        Stewarder result = stewarderRepository.save(stewarder);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, stewarder.getId().toString()))
            .body(result);
    }

    /**
     * GET  /stewarders : get all the stewarders.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of stewarders in body
     */
    @GetMapping("/stewarders")
    @Timed
    public List<Stewarder> getAllStewarders() {
        log.debug("REST request to get all Stewarders");
        return stewarderRepository.findAll();
    }

    /**
     * GET  /stewarders/:id : get the "id" stewarder.
     *
     * @param id the id of the stewarder to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the stewarder, or with status 404 (Not Found)
     */
    @GetMapping("/stewarders/{id}")
    @Timed
    public ResponseEntity<Stewarder> getStewarder(@PathVariable Long id) {
        log.debug("REST request to get Stewarder : {}", id);
        Optional<Stewarder> stewarder = stewarderRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(stewarder);
    }

    /**
     * DELETE  /stewarders/:id : delete the "id" stewarder.
     *
     * @param id the id of the stewarder to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/stewarders/{id}")
    @Timed
    public ResponseEntity<Void> deleteStewarder(@PathVariable Long id) {
        log.debug("REST request to delete Stewarder : {}", id);

        stewarderRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
